package com.helen.ui.newui;

import android.graphics.Bitmap;
import android.os.Build;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.helen.db.UserHelper;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.JsonUtil;
import com.helen.utils.ShareUtils;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yang on 2018/5/22.
 */

public class RoyaltyWebViewActivity extends BaseActivity {
    @BindView(R.id.webview)
    WebView webview;
    @BindView(R.id.img_share)
    ImageView imgShare;
    private String url;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_webview2;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(getIntent().getStringExtra("title"));
        webview.setWebViewClient(new MyWebViewClient());
        url = getIntent().getStringExtra("url");
        WebSettings ws = webview.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setUseWideViewPort(false);  //将图片调整到适合webview的大小
//        ws.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ws.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webview.loadUrl(url);
    }

    @Override
    protected void updateViews() {

    }


    private void shareUrl() {
        mMProgressDialog.show();
        HttpParams httpParams = new HttpParams();
        httpParams.put("id", UserHelper.getInstance().getloginEntity(RoyaltyWebViewActivity.this).getId());
        EasyHttp.post(Constants.API_JSONAPI_GET_SHARE_URL)
                .params(httpParams)
                .execute(new SimpleCallBack<String>() {
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        MToast.makeTextLong(RoyaltyWebViewActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String s) {
                        int code = JsonUtil.getJsonValuesInt(s, "code");
                        String message = JsonUtil.getJsonValuesString(s, "msg");
                        if (code == 0) {
                            String url = JsonUtil.getJsonValuesString(s, "url");
                            String QrCodeImg = JsonUtil.getJsonValuesString(s, "QrCodeImg");
//                            ShareUtils.showShare(RoyaltyWebViewActivity.this, url, "善回收", "分享有奖\n全国免费上门回收旧衣服,电子产品,书籍,家具等各种废旧物品", QrCodeImg);
                            ShareUtils.showShare(RoyaltyWebViewActivity.this, QrCodeImg);
                        } else {
                            MToast.makeTextLong(RoyaltyWebViewActivity.this, message).show();
                        }
                    }
                });
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
//            view.loadUrl(url);
            return super.shouldOverrideUrlLoading(view, url);

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webview.destroy();
    }

    @OnClick(R.id.rel_share)
    public void onViewClicked() {
        if (checkLoginState()) {
//            ShareUtils.showShare(this);
//            if (getIntent().getStringExtra("title").equals("推荐奖金提成表")) {
                shareUrl();
//            } else {
//                ShareUtils.showShare(this, Constants.APP_API_BASE + Constants.API_SHARE_MOBILE, "善回收", "分享有奖 金宝加1哟!", "");
//
//            }

        }

    }
}
